package com.jk.service.impl;

import com.jk.mapper.CouponMapper;
import com.jk.mapper.CouponOfUserMapper;
import com.jk.service.CouponService;
import com.jk.util.GetDateInfo;
import com.jk.vo.CouponInfo;
import com.jk.vo.CouponUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("couponService")
public class CouponServiceImpl implements CouponService{

    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private CouponOfUserMapper couponOfUserMapper;

    /**
     * 删除优惠劵
     * @param id
     */
    @Transactional(readOnly=false,propagation= Propagation.REQUIRED)
    public void deleteCoupon(String id) {
        System.out.println(id);
    }

    /**
     * 查询优惠劵
     * @param coupon（条件查询）
     * @return（返回对象）
     */
    public List<Map> queryCoupon(CouponInfo coupon) {

        List<Map> list = couponMapper.queryCoupon(coupon);

        return list;
    }

    public List<Map> queryUserCoupon(CouponInfo coupon) {

        List<Map> list = couponMapper.queryUserCoupon(coupon);

        return list;
    }

    /**
     * 给用户 新增优惠劵
     * @param couponUser aa
     */
    @Transactional(readOnly=false,propagation= Propagation.REQUIRED)
    public int addCouponOfUser(CouponUser couponUser) {
        /*查看是否已领优惠券*/
        int mas = couponOfUserMapper.queryCouponUser(couponUser);
        /*判断用户是否领过优惠券*/
        if(mas != 1){
            /*给用户添加优惠券*/
            int i = couponOfUserMapper.insertSelective(couponUser);
            /*判断是否新增成功*/
            if (i == 1) {
                /*新增成功后购物券数量减一*/
                int i1 = couponOfUserMapper.updateByPrimaryKey(couponUser.getCouponId());
                /*新增ok*/
                return 1;
            }else{
                /*新增失败*/
                return 2;
            }
        }else{
            /*购物券以领取过*/
            return 0;
        }
    }

    /**
     定时器删除
     */
    @Scheduled(cron = "0 0 12 * * *")
    @Transactional(readOnly=false,propagation= Propagation.REQUIRED)
    public void Timedelete(){
        /**
         * 获取七天前的时间
         */
        Date pastDate = GetDateInfo.getPastDate(6);

        int i = couponOfUserMapper.deleteByPrimaryKey(pastDate);

        int i2 = couponOfUserMapper.deleteByCouponInOfKey(pastDate);

    }

    /**
     * 定时修改状态
     */
    @Scheduled(cron = "0 0 12 * * *")
    @Transactional(readOnly=false,propagation= Propagation.REQUIRED)
    public void Timeupdata(){

        Date date = new Date();

        couponOfUserMapper.updateByPrimaryKeySelective(date);

        couponOfUserMapper.updateByCouponInOfKeySelective(date);
    }
    /**
     * 优惠劵使用完后修改状态
     */
    @Transactional(readOnly=false,propagation= Propagation.REQUIRED)
    public void updateUserCouponStatus(){

        CouponUser couponUser = new CouponUser();

        couponOfUserMapper.updateUserCouponStatus(couponUser);
    }
}
